package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.base.entity.Ozone;
import com.hkts.naturalgas.base.entity.RoastingDesulphurizing;
import com.hkts.naturalgas.base.entity.RoastingDust;
import com.hkts.naturalgas.base.entity.RoastingPurificationBleaching;
import com.hkts.naturalgas.base.mapper.RoastingDesulphurizingMapper;
import com.hkts.naturalgas.base.service.IRoastingDesulphurizingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.model.ResultData;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-31
 */
@Service
@Transactional
public class RoastingDesulphurizingServiceImpl extends ServiceImpl<RoastingDesulphurizingMapper, RoastingDesulphurizing> implements IRoastingDesulphurizingService {


    @Autowired
    private RoastingDesulphurizingMapper roastingDesulphurizingMapper;

    @Override
    public IPage<RoastingDesulphurizing> selectPage(Page<RoastingDesulphurizing> page, RoastingDesulphurizing roastingDesulphurizing) {
        QueryWrapper<RoastingDesulphurizing> query = Wrappers.query();
        QueryWrapper<RoastingDesulphurizing> entityWrapper = getEntityWrapper(query, roastingDesulphurizing);
        return roastingDesulphurizingMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<RoastingDesulphurizing> selectList(RoastingDesulphurizing roastingDesulphurizing) {
        QueryWrapper<RoastingDesulphurizing> query = Wrappers.query();
       // QueryWrapper<RoastingDesulphurizing> entityWrapper = getEntityWrapper(query, roastingDesulphurizing);
        return roastingDesulphurizingMapper.selectList(query);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<RoastingDesulphurizing> getEntityWrapper(QueryWrapper<RoastingDesulphurizing> entityWrapper, RoastingDesulphurizing roastingDesulphurizing) {
        //条件拼接
        entityWrapper.eq(RoastingDesulphurizing.TIME,roastingDesulphurizing.getTime());
        return entityWrapper;
    }
    @Override
    public IPage<RoastingDesulphurizing> selectPageByTime(Page<RoastingDesulphurizing> page, String startDate, String endDate){
        QueryWrapper<RoastingDesulphurizing> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(RoastingDesulphurizing::getTime);
        IPage<RoastingDesulphurizing> hktsTailGasPage = roastingDesulphurizingMapper.selectPage(page, query);
        return hktsTailGasPage;
    }

    @Override
    public IPage<RoastingDesulphurizing> getBrokenLineGraph(Page<RoastingDesulphurizing> page, String startDate, String endDate){
        QueryWrapper<RoastingDesulphurizing> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(RoastingDesulphurizing::getTime);
        IPage calcinationDesulfurizationDenitrations = roastingDesulphurizingMapper.selectPage(page,query);
        List<RoastingDesulphurizing> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(RoastingDesulphurizing vo){
        Map<String,Object> data = new LinkedHashMap<>();
        /*data.put("烟尘实测浓度(mg/m³)",vo.getDataFc());
        data.put("FT(m/s)",vo.getDataFt());*/
        data.put("浆液池高度(m)",vo.getDataLt());
        data.put("浆液密度(kg/m3)",vo.getDataNd());
        /*data.put("氮氧化物实测浓度(mg/m³)",vo.getDataNox());
        data.put("氧含量平均值(%)",vo.getDataO2());*/
        data.put("PH()",vo.getDataPh());
        /*data.put("PT(Kpa)",vo.getDataPt());*/
        data.put("除雾器压力(Kpa)",vo.getDataPt1003());
        /*data.put("二氧化硫实测浓度(mg/m³)",vo.getDataSo2());
        data.put("TE(℃)",vo.getDataTe());*/
        data.put("脱硫塔进口二车间烟气温度(℃)",vo.getDataTe1002());
        data.put("脱硫塔进口一车间烟气温度(℃)",vo.getDataTe1003());
        data.put("time",vo.getTime());
        return data;
    }

    @Override
    public void deleteAll() {
        roastingDesulphurizingMapper.deleteAll();
    }

    @Override
    public RoastingDesulphurizing getOneBytime(Date dateCellValue) {
        QueryWrapper<RoastingDesulphurizing> query = Wrappers.query();
        LocalDateTime ldt = dateCellValue.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        query.eq(RoastingDesulphurizing.TIME,ldt);
        List<RoastingDesulphurizing> roastingDesulphurizings = roastingDesulphurizingMapper.selectList(query);
        if(roastingDesulphurizings.size()==0){
            return null;
        }
        for(int i=0;i<roastingDesulphurizings.size();i++){
            if(i!=0){
                roastingDesulphurizingMapper.deleteById(roastingDesulphurizings.get(i).getId());
            }
        }
        return roastingDesulphurizings.get(0);
    }

    @Override
    public void handlerTimeDifferenceData(String format, String format1) {
        roastingDesulphurizingMapper.handlerTimeDifferenceData( format, format1);
    }


}
